package com.Web.menu;

import com.DAO.Impl.MenuDAOImpl;
import com.entity.Menu;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 20x
 * @date 2024 - 11 -13
 **/
@WebServlet("/SelectMenu")
public class MenuAddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();

        System.out.println("进入了servlet");
        String menuId = req.getParameter("menuId");
        Menu menu = new MenuDAOImpl().getMenuById(menuId);

        Map<String, Object> responseData = new HashMap<>();
        responseData.put("menu", menu);
        System.out.println(menu);

        // 将对象转换为JSON字符串
        String jsonResponse = gson.toJson(responseData);
        out.write(jsonResponse);
        out.close();
    }
}
